Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output an accurate error message when unimplemented math functions are called (globally) #1501

Merged
merged 1 commit into from
Apr 11, 2022

Conversation

sjinno
Copy link
Contributor

@sjinno sjinno commented Nov 1, 2021

An accurate error message wasn't output when unimplemented math functions were called.

In this case (#1498),

#version 450

const int x = clamp(1, 1, 1);

void main() {}

would say error: const values must have an initializer when it should actually be saying

error: Not implemented: Clamp

Note: This change only applies to unimplemented math functions that are called globally (or should I say externally?).

Feedback on wording/how code should be written is very much appreciated! :)

@jimblandy jimblandy self-requested a review November 1, 2021 15:33
@jimblandy
Copy link
Member

(Taking review, since I'm mentoring @sjinno on this work.)

Copy link
Member

@jimblandy jimblandy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good - thank you very much!

There are just a few things I'd like changed.

@@ -368,7 +368,7 @@ impl Parser {

pub struct DeclarationContext<'ctx> {
qualifiers: Vec<(TypeQualifier, Span)>,
external: bool,
external: bool, // Indicates global declaration
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though DeclarationContext is private, and will not appear in documentation unless someone builds with --document-private-items, I think this should still be a /// doc comment, placed above the field, because that's what people are expecting to see.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay!

init.and_then(|(root, meta)| parser.solve_constant(ctx.ctx, root, meta).ok());
let maybe_constant = if let Some((root, meta)) = init {
let result = parser.solve_constant(ctx.ctx, root, meta);
match (result, ctx.external) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks correct, but I think it might be clearer as a plain match result, with a guard expression on the Err case:

Err(err) if ctx.external => return Err(err),

It also deserves a little comment, something like:

Initializers for external declarations must be constants, so if solve_constant failed, that's an error the user should see. But if it's an initializer for a local variable, then failing to produce a constant just means we emit the expression as code; that is not an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for the review! I'd like to use what you wrote as a comment. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently maybe_constant assigns the returned value of sove_constant for both externally and internally declared math functions if implemented.

So, should it also be

Ok(res) if ctx.external => Some(res)

so that internally declared math expressions are left as is?

Copy link
Collaborator

@JCapucho JCapucho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a problem with your current approach that makes valid shaders not pass

src/front/glsl/parser/declarations.rs Outdated Show resolved Hide resolved
@sjinno sjinno force-pushed the 1498-print-proper-error-message branch 2 times, most recently from 648ab6c to 173ab74 Compare November 8, 2021 01:16
@sjinno sjinno force-pushed the 1498-print-proper-error-message branch from 9ac3f3c to cd5fbdb Compare November 10, 2021 03:17
Copy link
Collaborator

@JCapucho JCapucho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation wise it looks good to me, I'll let @jimblandy handle the rest since he is mentoring you

@sjinno
Copy link
Contributor Author

sjinno commented Nov 10, 2021

@JCapucho Thank you for your help!

@sjinno sjinno closed this Nov 21, 2021
@sjinno sjinno reopened this Nov 21, 2021
Co-authored-by: JCapucho <jcapucho7@gmail.com>
@JCapucho JCapucho force-pushed the 1498-print-proper-error-message branch from a4ac636 to 55e8379 Compare March 17, 2022 17:04
@JCapucho
Copy link
Collaborator

Since the author hasn't touched this PR in a while, I've gone ahead and rebased it and cleaned it up a little, the code is essentially the same, only that now the constants are also folded for non const declarations (as was previously done), I reworded the comments a bit as was requested by jimb in his review and changed the error message to be a bit more helpful.

I've also cleaned the git history since it picked up many unneeded commits.

@jimblandy it still needs an approval from you, so if you could do a review it would be very helpful.

@JCapucho JCapucho requested a review from jimblandy March 25, 2022 23:10
Copy link
Member

@jimblandy jimblandy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me - thank you very much!

@jimblandy jimblandy merged commit 8f5d8f6 into gfx-rs:master Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants